import requests
import pymongo
import json
import random
import time
from fake_useragent import UserAgent

clien=pymongo.MongoClient(host='改成自己的数据库')
db=clien.Blind_date_information
coll=db.text

ua=UserAgent()

headers={
    'User-Agent':ua.random
}

def request_page_html(page):
    URL='http://www.7799520.com/api/user/pc/list/search?marry=1&page={}'.format(page)
    try:
        sponse=requests.get(URL,headers=headers)
        return json.loads(sponse.text)
    except Exception:
        print('request error')

def extract_Json(json):
    if json:
        jsons=json.get('data').get('list')
        for i in jsons:
            data={}
            data['相亲人的照片']=i.get('avatar')
            data['出生年月']=i.get('birthdayyear')
            data['学历']=i.get('education')
            data['独白']=i.get('monolog')
            data['网站中的网名']=i.get('username')
            data['UserID']=i.get('userid')
            data['所在省份']=i.get('province')
            data['所在城市']=i.get('city')
            data['身高']=i.get('height')
            data['性别(1代表男 2代表女)']=i.get('gender')
            coll.insert_one(data)
            print(data)

#一共2872页
#print(request_page_html(2872))
#extract_Json(request_page_html(1))

for i in range(1,2873):
    extract_Json(request_page_html(i))